<?php 
namespace app\api\controller\v1;

use think\Db;
//登录后基类
class BalanceController extends UserBaseController{

	function  demo(){
		return '成功';
	} 

	protected function deal_balance($rows){
		foreach ($rows as $key => &$value) {
			$value['create_at'] = date('Y-m-d H:i',$value['create_at']);
			unset($value['balance_type']);
			unset($value['table']);
			unset($value['table_id']);
		}
		return $rows;
	}

	//所有
	function list(){
		$map['user_id']     = $this->user['id'];
		$data=input('param.');
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = model('balance')
				->where($map)
				->limit($start,$pagesize)
				->order('create_at desc')
				->select()
				->toArray();
		$rows = $this->deal_balance($rows);

		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}



	//激活奖励
	function activeback(){
		$map['type']        =11;
		$map['user_id']     = $this->user['id'];
		$data=input('param.');
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = model('balance')
				->where($map)
				->limit($start,$pagesize)
				->order('create_at desc')
				->select()
				->toArray();
		$rows = $this->deal_balance($rows);

		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}



	//激活返还统计
	function activebackstatic(){
		$ret=[];
		$map['type']        =11;
		$map['user_id']     = $this->user['id'];
		$ret['total'] = model('balance')->where($map)->sum('amount');
		$date = trim(input('param.date/s'));
		$date = preg_match('#^\d{4}\-\d{1,2}\-\d{1,2}$#',$date) ? $date : Date('Y-m-d');
		$start_ts = strtotime($date);
		$end_ts   = $start_ts + 24*3600;
		$map['create_at'] =['>=',$start_ts];
		$map['create_at'] =['<',$end_ts];
		$ret['date_total'] = model('balance')->where($map)->sum('amount');
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$ret]);
	}


	//奖励返还列表
	function profitback(){
		$map['type']        =12;
		$map['user_id']     = $this->user['id'];
		$data=input('param.');
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = model('balance')
				->where($map)
				->limit($start,$pagesize)
				->order('create_at desc')
				->select()
				->toArray();
		$rows = $this->deal_balance($rows);

		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}


	//奖励返还统计
	function profitbackstatic(){
		$ret=[];
		$map['type']        =12;
		$map['user_id']     = $this->user['id'];
		$ret['total'] = model('balance')->where($map)->sum('amount');
		$date = trim(input('param.date/s'));
		$date = preg_match('#^\d{4}\-\d{1,2}\-\d{1,2}$#',$date) ? $date : Date('Y-m-d');
		$start_ts = strtotime($date);
		$end_ts   = $start_ts + 24*3600;
		$map['create_at'] =['>=',$start_ts];
		$map['create_at'] =['<',$end_ts];
		$ret['date_total'] = model('balance')->where($map)->sum('amount');
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$ret]);
	}


	/**
	 * [withdrawapply 提现申请]
	 * @Author   思睿软件
	 * @DateTime 2021-07-18T12:02:15+0800
	 * @Version  1.0
	 * @return   [type]                   [description]
	 */
	function withdrawapply(){
		$data= input('param.');
		if($data['amount']<=0){
			return json(['error'=>1,'msg'=>$this->lang['AMOUNT_ERROR']]);
		}
		try {
			model('withdraw')
				->insert([
					'user_id'   =>$this->user['id'],
					'amount'    =>$data['amount'],
					'create_at' =>time()
				]);
			return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS']]);
		} catch (Exception $e) {
			return json(['error'=>1,'msg'=>$this->lang['OPERATE'].$this->lang['FAIL']]);
		}
	}



	//充值列表  事实上不存在 未到账的状态
	function recharge(){
		$map['user_id']     = $this->user['id'];
		$data=input('param.');
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = model('recharge')
				->where($map)
				->limit($start,$pagesize)
				->order('create_at desc')
				->select()
				->toArray();
		foreach ($rows as $key => &$value) {
			$value['status_title']  = app\common\model\RechargeModel::$titles($value['status']);
			$value['create_at']     = date('Y-m-d H:i',$value['create_at']);
		}
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}




	function withdraw(){
		$map['user_id']     = $this->user['id'];
		$data=input('param.');
		$page = $data['page']<=1 ? 1: $data['page'];
		$pagesize= $data['pagesize']<=0? 10: $data['pagesize'];
		$start = ($page-1) * $pagesize;
		$rows = model('withdraw')
				->where($map)
				->limit($start,$pagesize)
				->order('create_at desc')
				->select()
				->toArray();
		foreach ($rows as $key => &$value) {
			$value['status_title']  = app\common\model\RechargeModel::$titles($value['status']);
			$value['create_at']     = date('Y-m-d H:i',$value['create_at']);
		}
		return json(['error'=>0,'msg'=>$this->lang['OPERATE'].$this->lang['SUCCESS'],'data'=>$rows]);
	}



}